# -*- coding: utf-8 -*-
# @Time: 2022/9/10 14:09
# @File: pipe.py
import pygame
from setting import *
from random import randint


class Pipe(pygame.sprite.Sprite):

    def __init__(self, group):
        super().__init__(group)
        self.screen = pygame.display.get_surface()  # 屏幕对象

        # 随机设定上下水管的长度
        self.top_length = SCREEN_HEIGHT * randint(*PIPE_PROPORTION) // 100
        self.bottom_length = SCREEN_HEIGHT * randint(*PIPE_PROPORTION) // 100
        self.x = SCREEN_WIDTH  # 水管的水平坐标
        # 水管的图像由多张图片拼接而成，用于记录每张图片的垂直坐标
        self.top_y_list = list()
        self.bottom_y_list = list()
        # 用于存储碰撞矩形对象的参数
        self.collision_area = list()
        self.speed = -PIPE_SPEED  # 水管的水平移动速度

        # 加载水管的图片对象
        self.top_surface = pygame.image.load(
            "../material/image/pipe_top.png")
        self.top_end_surface = pygame.image.load(
            "../material/image/pipe_top_end.png")
        self.bottom_surface = pygame.image.load(
            "../material/image/pipe_bottom.png")
        self.bottom_end_surface = pygame.image.load(
            "../material/image/pipe_bottom_end.png")

        self.count_y_coordinate()  # 计算每张图片的垂直坐标
        self.create_collision_area()  # 创建碰撞矩形

    def count_y_coordinate(self):
        # 计算上边水管的y坐标
        top_y = self.top_length - self.top_end_surface.get_height()
        self.top_y_list.append(top_y)
        while top_y > 0:
            top_y -= self.top_surface.get_height()
            self.top_y_list.append(top_y)

        # 计算下边水管的y坐标
        bottom_y = SCREEN_HEIGHT - self.bottom_length
        self.bottom_y_list.append(bottom_y)
        bottom_y += self.bottom_end_surface.get_height()
        while bottom_y <= SCREEN_HEIGHT:
            self.bottom_y_list.append(bottom_y)
            bottom_y += self.bottom_surface.get_height()

    def create_collision_area(self):
        # 添加上边水管的碰撞矩形
        rect = self.top_end_surface.get_rect(
            topleft=(self.x, self.top_y_list[0]))
        self.collision_area.append(rect)
        for y in self.top_y_list[1:]:
            self.collision_area.append(
                self.top_surface.get_rect(topleft=(self.x, y)))

        # 添加下边水管的碰撞矩形
        rect = self.bottom_end_surface.get_rect(
            topleft=(self.x, self.bottom_y_list[0]))
        self.collision_area.append(rect)
        for y in self.bottom_y_list[1:]:
            self.collision_area.append(
                self.bottom_surface.get_rect(topleft=(self.x, y)))

    def update_collision_area(self):
        for rect in self.collision_area:
            rect.left = self.x

    def draw_pipe(self):
        # 绘制上边的水管
        self.screen.blit(self.top_end_surface, (self.x, self.top_y_list[0]))
        for y in self.top_y_list[1:]:
            self.screen.blit(self.top_surface, (self.x, y))

        # 绘制下边的水管
        self.screen.blit(self.bottom_end_surface,
                         (self.x, self.bottom_y_list[0]))
        for y in self.bottom_y_list[1:]:
            self.screen.blit(self.bottom_surface, (self.x, y))

    def update(self, duration_time, move=True):
        if move:  # 更新水平坐标实现移动
            self.x -= PIPE_SPEED * duration_time
        # 如果水管移动到屏幕左侧外，删除该对象
        if self.x + PIPE_IMAGE_WIDTH <= 0:
            self.kill()

        self.draw_pipe()  # 绘制水管
        self.update_collision_area()  # 更新碰撞矩形的位置
